package red.lightblue.admin.service.impl;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import red.lightblue.admin.dao.TypeDAO;
import red.lightblue.admin.dto.TypeDTO;
import red.lightblue.admin.model.Type;
import red.lightblue.admin.qc.TypeQC;
import red.lightblue.admin.service.TypeService;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class TypeServiceImpl implements TypeService {

    @Autowired
    private TypeDAO typeDAO;

    @Override
    public List<TypeDTO> getAllProductType() {
        List<Type> types = typeDAO.listType(new TypeQC());
        List<TypeDTO> typeDTOS = types.stream().map(type->
        {TypeDTO dto = new TypeDTO();BeanUtils.copyProperties(type,dto);
            return  dto;}).collect(Collectors.toList());
        return typeDTOS;
    }

    @Override
    public List<TypeDTO> getChildrenProductType(Integer id) {
        TypeQC typeQC = new TypeQC();
        typeQC.setParentTypeId(id);
        List<Type> types = typeDAO.listType(typeQC);
        List<TypeDTO> typeDTOS = types.stream().map(type->
        {TypeDTO dto = new TypeDTO();BeanUtils.copyProperties(type,dto);
            return  dto;}).collect(Collectors.toList());
        return typeDTOS;
    }
}